We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 13 |
Total Lines | 60 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jquery.hotkeys.js ➔ keyHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
35 | function keyHandler( handleObj ) { |
||
36 | // Only care when a possible input has been specified |
||
37 | if ( typeof handleObj.data !== "string" ) { |
||
38 | return; |
||
39 | } |
||
40 | |||
41 | var origHandler = handleObj.handler, |
||
42 | keys = handleObj.data.toLowerCase().split(" "), |
||
43 | textAcceptingInputTypes = ["text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime", "datetime-local", "search", "color", "tel"]; |
||
44 | |||
45 | handleObj.handler = function( event ) { |
||
46 | // Don't fire in text-accepting inputs that we didn't directly bind to |
||
47 | if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) || |
||
48 | jQuery.inArray(event.target.type, textAcceptingInputTypes) > -1 ) ) { |
||
49 | return; |
||
|
|||
50 | } |
||
51 | |||
52 | // Keypress represents characters, not special keys |
||
53 | var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ], |
||
54 | character = String.fromCharCode( event.which ).toLowerCase(), |
||
55 | key, modif = "", possible = {}; |
||
56 | |||
57 | // check combinations (alt|ctrl|shift+anything) |
||
58 | if ( event.altKey && special !== "alt" ) { |
||
59 | modif += "alt+"; |
||
60 | } |
||
61 | |||
62 | if ( event.ctrlKey && special !== "ctrl" ) { |
||
63 | modif += "ctrl+"; |
||
64 | } |
||
65 | |||
66 | // TODO: Need to make sure this works consistently across platforms |
||
67 | if ( event.metaKey && !event.ctrlKey && special !== "meta" ) { |
||
68 | modif += "meta+"; |
||
69 | } |
||
70 | |||
71 | if ( event.shiftKey && special !== "shift" ) { |
||
72 | modif += "shift+"; |
||
73 | } |
||
74 | |||
75 | if ( special ) { |
||
76 | possible[ modif + special ] = true; |
||
77 | |||
78 | } else { |
||
79 | possible[ modif + character ] = true; |
||
80 | possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true; |
||
81 | |||
82 | // "$" can be triggered as "Shift+4" or "Shift+$" or just "$" |
||
83 | if ( modif === "shift+" ) { |
||
84 | possible[ jQuery.hotkeys.shiftNums[ character ] ] = true; |
||
85 | } |
||
86 | } |
||
87 | |||
88 | for ( var i = 0, l = keys.length; i < l; i++ ) { |
||
89 | if ( possible[ keys[i] ] ) { |
||
90 | return origHandler.apply( this, arguments ); |
||
91 | } |
||
92 | } |
||
93 | }; |
||
94 | } |
||
95 | |||
100 | })( jQuery ); |